package com.base.controller;

import com.base.po.voip.Users;
import com.base.service.COUPON.CouponOrderNewService;
import com.base.service.UsersService;
import com.base.utils.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author XiFYuW
 * @date 2020/07/13 13:42
 */
@Controller
@RequestMapping("/admin")
public class HvMemberController {


    private final CouponOrderNewService couponGoodsNewService;

    private final UsersService usersService;

    public HvMemberController(CouponOrderNewService couponGoodsNewService, UsersService usersService) {
        this.couponGoodsNewService = couponGoodsNewService;
        this.usersService = usersService;
    }

    @RequestMapping("/hv_member")
    public ModelAndView index(HttpServletRequest request, Integer currentPageNo, String status, String mobile,
                              String userName, String createTime) throws Exception{
        Map<String, Object> map = new HashMap<>(); // 返回数据对象
        Map<String, Object> param = getParam(mobile, userName, createTime);//条件查询参数
        String adminUserId = (String) request.getSession().getAttribute("adminUserId");
        if (adminUserId == null) {
            return new ModelAndView("jsp/hv_member", map);
        }
        // 设置分页 获取内容
        Page page = new Page(); // 实例化分页
        if (currentPageNo != null) {
            page.setCurrentPageNo(currentPageNo); // 设置当前页
        }
        int recordTotal = couponGoodsNewService.countALLLeftJoinHv(param);
        page.setRecordTotal(recordTotal);
        List<Map<String,Object>> list = couponGoodsNewService.selectALLLeftJoinHv(page, param);
        list = list.stream().peek(x -> {
            String parentId = String.valueOf(x.get("parentId"));
            peekUserName(x, parentId);
        }).collect(Collectors.toList());
        map.put("hvMember",list); // 数据集合
        map.put("status", status); // 状态
        map.put("pages", page); // 分页
        return new ModelAndView("jsp/hv_member", map);
    }

    private void peekUserName(Map<String, Object> map, String parentId) {
        Users users = usersService.selectByPrimaryKey(parentId);
        if(users != null){
            map.put("userName", users.getUsername());
        }
    }

    private Map<String, Object> getParam(String mobile, String userName, String createTime){
        Map<String, Object> map = new HashMap<>();


        if (mobile != null && !mobile.equals("")) {
            map.put("mobile", mobile);
        }

        if (userName != null && !userName.equals("")) {
            Users users = usersService.selectByUsername(userName);
            if(users != null){
                map.put("parentId", users.getId());
            }
        }


        if (createTime != null && !createTime.equals("")){
            String[] str = createTime.split(" - ");
            map.put("createTime1", str[0]);
            map.put("createTime2", str[1]);
        }
        return map;
    }
}
